﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NetMove01
{
    public partial class Form1 : Form
    {

        NetBase nb;
        SendData sd = new SendData();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            nb = new NetServer();
            nb.Start(ERole.Server);
            label1.Text = "Connected";
        }

        private void button2_Click(object sender, EventArgs e)
        {
        
            nb = new NetClient();
            nb.Start(ERole.Client);
            label1.Text = "Connected";

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (nb != null) 
            {
                nb.Send(sd);
                SendData otherSd;
                nb.Receive(out otherSd);

                switch (nb.Role)
                {
                    case ERole.Server:
                        p2.Left = otherSd.x;
                        p2.Top = otherSd.y;
                        break;
                    case ERole.Client:
                        p1.Left = otherSd.x;
                        p1.Top = otherSd.y;
                        break;
                }


            }
        }


        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (nb != null)
            {
                sd.x = e.X;
                sd.y = e.Y;

                switch (nb.Role)
                {
                    case ERole.Server:
                        p1.Left = sd.x;
                        p1.Top = sd.y;
                        break;
                    case ERole.Client:
                        p2.Left = sd.x;
                        p2.Top = sd.y;
                        break;

                }
            }

        }
    }
}